package com.wsoft.core.utils;

import java.util.Stack;

/**
 * 流程变量（控制切换组织表）
 *
 * @Author zhonggukai
 * @Date 2022/8/17 21:48
 */
public class SqlAuthOrgHolder {
    private final static ThreadLocal<Stack> ORGHOLDER = new ThreadLocal<>();

    public static void open() {
        if (ORGHOLDER.get() == null) {
            // 每个线程初始化一个堆栈
            ORGHOLDER.set(new Stack());
        }
        ORGHOLDER.get().push(true);
    }

    public static boolean needSwitch() {
        Stack stack = ORGHOLDER.get();
        // 判断当前线程stack是否有值即可
        return stack == null || stack.isEmpty();
    }

    public static void close() {
        // 退出当前层
        Stack stack = ORGHOLDER.get();
        if (stack != null && !stack.isEmpty()) {
            stack.pop();
        }
    }

    /**
     * 清空当前线程权限堆栈
     *
     * @param
     * @return void
     * @author zhonglj
     * @date 2023/11/22 16:47
     **/
    public static void clearStack() {
        Stack stack = ORGHOLDER.get();
        if (stack != null && !stack.isEmpty()) {
            stack.clear();
        }
    }
}
